import { createRouter, createWebHashHistory } from 'vue-router'
import { showFailToast } from "vant";
// 路由配置
const routes = [
  { path: '/', redirect: '/home' },
  {
    path: '/layout',
    name: 'Layout',
    component: () => import('@/views/layout/index.vue'),
    meta: {
      title: '布局'
    },
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/home/index.vue'),
        meta: {
          title: '首页',
          keepAlive: true // 启用缓存
        }
      },
      {
        path: '/cart',
        name: 'Cart',
        component: () => import('@/views/cart/index.vue'),
        meta: {
          title: '购物车',
          keepAlive: false, // 不启用缓存
          requiresAuth: true // 需要登录
        }
      },
      {
        path: '/order',
        name: 'Order',
        component: () => import('@/views/order/index.vue'),
        meta: {
          title: '订单',
          keepAlive: true,
          requiresAuth: true // 需要登录
        }
      },
      {
        path: '/mine',
        name: 'Mine',
        component: () => import('@/views/mine/index.vue'),
        meta: {
          title: '我的',
          keepAlive: true,
          requiresAuth: true // 需要登录
        }
      }
    ]
  },
  {path:'/food',
  name:'HomeFood',
  component:()=>import('@/views/test/food.vue'),
   meta: {
          title: '美食',
          keepAlive: false // 不启用缓存
        },
  },
  {
    path: '/login',
    name:'Login',
    component:()=>import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      keepAlive: false // 不启用缓存
    },
  },
  {
    path: '/register',
    name:'Register',
    component:()=>import('@/views/register/index.vue'),
    meta: {
      title: '注册',
      keepAlive: false // 不启用缓存
    },
  },
  // 商家页
  {
    path: '/shopper',
    name:'Shopper',
    component:()=>import('@/views/shopper/index.vue'),
    meta: {
      title: '商家',
      keepAlive: false // 不启用缓存
    }
  },
  // 创建订单页
  {
    path: '/payment',
    name:'Payment',
    component:()=>import('@/views/payment/index.vue'),
    meta: {
      title: '创建订单',
      keepAlive: false
    }
  },
  // 地址编辑页
  {
    path: '/locationEdit',
    name:'Location',
    component:()=>import('@/views/location/LocationEdit.vue'),
    meta: {
      title: '地址编辑',
      keepAlive: false
    }
  },
  // 地址列表页
  {
    path: '/locationList',
    name:'LocationList',
    component:()=>import('@/views/location/LocationList.vue'),
    meta: {
      title: '地址列表',
      keepAlive: false
    }
  },
  {
    path: '/404',
    name:'NotFound',
    component:()=>import('@/views/404/index.vue'),
    meta: {
      title: '404',
      keepAlive: false
    }
  },
  // 其他未匹配路径都重定向到404
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }

]

// 创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫
// 辅助函数 
const checkAuth = (to, token) => { 
  const needAuth = to.matched.some(r => r.meta?.requiresAuth) 
  const isLoginPage = to.path === '/login' || to.path === '/register'
  
  // 未登录且需要登录的情况 
  if (needAuth && !token) { 
    showFailToast('请先登录')
    return '/login' 
  } 
  // 已登录且访问登录页的情况 
  if (token && isLoginPage) { 
    showFailToast('已登录，请勿重复登录')
    return '/home' 
  } 
  
  return null // 放行 
} 

router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title + ' - fan外卖'
  }
  
  const token = localStorage.getItem('fanfan-waimai-h5-master-token')
  const redirect = checkAuth(to, token)
  
  next(redirect || undefined)
})

export default router